//------------------------------------------------------------------------------
// <copyright company="Telligent Systems">
//     Copyright (c) Telligent Systems Corporation.  All rights reserved.
// </copyright> 
//------------------------------------------------------------------------------

using System;
using System.Collections;
using System.Collections.Specialized;
using System.Text.RegularExpressions;
using System.Web;
using System.Configuration;
using System.Web.UI;
using System.Web.UI.Design;
using System.Web.UI.WebControls;
using CommunityServer;
using CommunityServer.Components;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.IO;

namespace CommunityServer.Controls {
    [
    ParseChildren(true),
    Designer(typeof(AlphaPickerDesigner))
    ]
    public class AlphaPicker : WebControl, INamingContainer {

        string currentLetter = null;// ResourceManager.GetString("AlphaPicker_All");
        string spacer = ResourceManager.GetString("AlphaPicker_Spacer");

        // *********************************************************************
        //  CreateChildControls
        //
        /// <summary>
        /// Renders the Alpha picker options
        /// </summary>
        /// 
        // ********************************************************************/ 
        protected override void CreateChildControls() {

            // If spacer is nothing
            //
            if (spacer == "")
                spacer = "&nbsp;";

            // Add the series of linkbuttons
            char chrStart = 'A';
            char chrStop = 'Z';

            // Loop through all the characters
            for (int iLoop = chrStart; iLoop <= chrStop; iLoop++) {
                Controls.Add(CreateLetteredLinkButton(((char) iLoop).ToString()));


                Controls.Add(new LiteralControl(spacer));
            }

            Controls.Add(CreateLetteredLinkButton(ResourceManager.GetString("AlphaPicker_All")));

        }

        // *********************************************************************
        //  CreateLetteredLinkButton
        //
        /// <summary>
        /// Creates link buttons
        /// </summary>
        /// 
        // ********************************************************************/ 
        private LiteralOrLink CreateLetteredLinkButton(String buttonText) {

            // Add a new link button
            LiteralOrLink btnTmp = new LiteralOrLink();
            btnTmp.Text = buttonText;

            if (buttonText == ResourceManager.GetString("AlphaPicker_All"))
            {
                btnTmp.NavigateUrl = Context.Request.Path + "?a=" + "true";
                if(IsSelectAll)
                    btnTmp.TextOnly = true;
            }
            else
                btnTmp.NavigateUrl = Context.Request.Path + string.Format("?q={0}{1}",PreText,buttonText);

            if(SelectedLetter != null && SelectedLetter.ToLower() == buttonText.ToLower())
                btnTmp.TextOnly = true;

            return btnTmp;
        }


        // *********************************************************************
        //  SelectedLetter
        //
        /// <summary>
        /// Property that returns the currently selected Letter
        /// </summary>
        /// 
        // ********************************************************************/
        public string SelectedLetter {
            get {
                if (currentLetter == null)
                {
                    currentLetter = Context.Request.QueryString["q"];
                    if(!Globals.IsNullorEmpty(this.PreText) && currentLetter != null && currentLetter.Length > 0)
                        currentLetter =  currentLetter.Substring(PreText.Length); 
                }
                return currentLetter;
            }
            set {
                currentLetter = value;
            }
        }

        public string FilterText 
        {
            get 
            {
                 return Context.Request.QueryString["q"];
            }

        }

        public bool IsSelectAll
        {
            get
            {
                return Context.Request.QueryString["a"] == "true";
            }
        }

        private string _preText = "^";

        public string PreText
        {
            get
            {
                return _preText;
            }
            set
            {
                _preText = value;
            }
        }
    }

    public class AlphaPickerDesigner : ControlDesigner {

        public override string GetDesignTimeHtml() {
            // Create the controls
            //
            ControlCollection controls = ((Control) Component).Controls;

            return "TODO: Design time";

        }

        public override void Initialize(IComponent component) {
            base.Initialize(component);
        }
    }
}
